<script>
import { mapState } from 'vuex'
import TypeForm from '@/views/business/goodsType/components/typeForm.vue'

export default {
  name: 'goodsType',
  components: { TypeForm },
  data() {
    return {
      pageParams: {
        page: 1,
        pageSize: 10,
        name: undefined,
        status: undefined
      },
      loading: false,
      dialogVisible: false,
      edit: false,
      statusArray: [
        {
          type: 0,
          label: '禁用'
        },
        {
          type: 1,
          label: '正常'
        }
      ]
    }
  },
  methods: {
    /*
    * 获取货物类型分页数据的回调函数
    *  */
    async fetchGoodsTypeList() {
      try {
        this.loading = true
        await this.$store.dispatch('goodsTypeStore/fetchGoodsTypeListAction', this.pageParams)
      } catch (error) {
        this.$message.error(error.message)
      } finally {
        this.loading = false
      }
    },
    /*
    *pageSize发生变化的回调函数
    *  */
    handleSizeChange(pageSize) {
      this.pageParams.pageSize = pageSize
      this.fetchGoodsTypeList()

    },
    /*
    *page发生变化的回调函数
    *  */
    handleCurrentChange(page) {
      this.pageParams.page = page
      this.fetchGoodsTypeList()
    },
    /*
    * 搜索按钮的回调函数
    *  */
    handleSearch() {
      this.pageParams.page=1
      this.fetchGoodsTypeList()
    },
    /*
    * 点击重置按钮的回调函数
    *  */
    handleReset() {
      this.pageParams.status = undefined
      this.pageParams.name = undefined
      this.fetchGoodsTypeList()
    },
    /*
    * 删除货物类型的回调函数
    *  */
    async handleDelete(row) {
      const confirm = await this.$confirm('此操作将永久删除货物类型, 是否继续?', '提示', { confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning' }).catch(error => error)
      if (confirm === 'cancel') return
      try {
        const deleteResult = await this.$store.dispatch('goodsTypeStore/deleteGoodsTypeAction', row.id)
        await this.fetchGoodsTypeList()
        this.$message.success(deleteResult)
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    /*
    * 新增货物类型的回调函数
    *  */
    handleAdd() {
      this.dialogVisible = true
      this.edit = false
    },
    /*
    * 点击编辑按钮回调函数
    *  */
    handleEdit(row) {
      this.dialogVisible = true
      this.edit = true
      this.$nextTick(() => {
        this.$refs.typeForm.fetchGoodsTypeDetail(row.id)
      })
    },
    /*
    *
    * 点击确定按钮的回调函数
    *  */
    async handleSubmit() {
      if (this.edit) {
        try {
          const editResult = await this.$store.dispatch('goodsTypeStore/editGoodsTypeAction', this.$refs.typeForm.formData)
          this.dialogVisible = false
          await this.fetchGoodsTypeList()
          this.$message.success(editResult)
        } catch (error) {
          this.$message.error(error.message)
        }
      } else {
        try {
          const addResult = await this.$store.dispatch('goodsTypeStore/addGoodsTypeAction', this.$refs.typeForm.formData)
          this.dialogVisible = false
          await this.fetchGoodsTypeList()
          this.$message.success(addResult)
        } catch (error) {
          this.$message.error(error.message)
        }
      }
    }
  },
  computed: {
    ...mapState('goodsTypeStore', ['counts', 'goodsTypeList']),
    title() {
      return this.edit ? '编辑货物类型' : '添加货物类型'
    }
  },
  mounted() {
    this.fetchGoodsTypeList()
  }
}
</script>

<template>
  <div class="goods-type">
    <el-card shadow="never" class="top">
      <el-form inline :model="pageParams">
        <el-form-item label="货物类型名称">
          <el-input placeholder="请输入货物类型名称" v-model="pageParams.name"></el-input>
        </el-form-item>
        <el-form-item label="货物类型状态">
          <el-select v-model="pageParams.status">
            <el-option v-for="(item) in statusArray" :value="item.type" :label="item.label"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button icon="el-icon-search" type="primary" @click="handleSearch">搜索</el-button>
          <el-button icon="el-icon-refresh" @click="handleReset">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card shadow="never" class="bottom" v-loading="loading" element-loading-text="拼命加载中" element-loading-spinner="el-icon-loading" element-loading-background="rgba(255,255,255)">
      <div class="header">
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">新增货物类型</el-button>
      </div>
      <el-table border stripe :data="goodsTypeList" :header-cell-style="{background:'#f4f6f8',color:'#969393'}">
        <el-table-column type="index" label="序号" align="center" width="50px"></el-table-column>
        <el-table-column prop="name" label="货物类型名称" align="center"></el-table-column>
        <el-table-column prop="remark" label="说明" align="center"></el-table-column>
        <el-table-column prop="status" label="状态" align="center">
          <template #default="{row}">{{ row.status === 1 ? '正常' : '禁用' }}</template>
        </el-table-column>
        <el-table-column prop="defaultWeight" label="重量(千克)" align="center"></el-table-column>
        <el-table-column prop="defaultVolume" label="体积(立方)" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template #default="{row}">
            <el-button type="text" @click="handleEdit(row)">编辑</el-button>
            <el-button type="text" @click="handleDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParams.page" :page-sizes="[10, 15, 20, 25]" :page-size="pageParams.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="counts" background></el-pagination>
    </el-card>
    <el-dialog :title="title" :visible.sync="dialogVisible" @close="$refs.typeForm.clearFormData()">
      <type-form ref="typeForm"></type-form>
      <template #footer>
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleSubmit">确 定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped lang="scss">
.goods-type {
  padding: 20px;

  .bottom {
    margin-top: 10px;

    .el-table {
      margin: 10px 0;
    }

    .el-pagination {
      display: flex;
      justify-content: center;
    }
  }
}
</style>
